package com.itheima.d9BinarySearch;

public class Test {
    public static void main(String[] args) {
        // 一定是排好序的
        int[] numbers = {5,6,10,24,56,78,100};
        int i = MyBinarySearch(numbers, 101);
        System.out.println("元素索引为:"+i);
    }

    /**
     *
     * @param numbers  排序的数组
     * @param findData  要找的数据
     */
    public static int MyBinarySearch(int[] numbers,int findData){
        int low = 0; // 定义左位置
        int high = numbers.length-1;  // 定义右位置
        while (low<=high){
            int mid = (low+high)/2;
            // 比较中间元素的值
            if(numbers[mid] > findData){
                high = mid -1;
            }else if(numbers[mid]<findData){
                low = mid+1;
            }else
                {
            return  mid;
            }

        }
        return -1;
    }
}
